<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生命周期</title>
</head>
<body>
    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class Image extends React.Component{
            render(){
                console.log('4-渲染中...')
                return <img src={"https://source.unsplash.com/500x400?" + this.props.keyword} alt="" />
            }
            //1. 组件将要接受属性参数
            componentWillReceiveProps(){
                console.log('1-组件将要接受数据')
            }

            //2.组件应该要更新了
            shouldComponentUpdate(){
                console.log('2-组件应该要更新了...');
                //需要返回布尔类型的值, 决定后续回调的执行
                return true;
            }

            //组件将要更新
            componentWillUpdate(){
                console.log('3-组件将要更新...');
            }

            //组件完成了更新
            componentDidUpdate(){
                console.log('5-组件完成了更新...');
            }
        }

        class App extends React.Component{
            state = {
                keyword: 'html5'
            }

            render(){
                this.setState({
                    keyword: 'test'
                })
                return <div>
                        <button k="animal" onClick={this.change}>动物</button>&nbsp;&nbsp;
                        <button k="car" onClick={this.change}>汽车</button>&nbsp;&nbsp;
                        <button k="game" onClick={this.change}>游戏</button>&nbsp;&nbsp;
                        <hr />
                        <Image keyword={this.state.keyword} />
                    </div>
            }

            //声明按钮的单击实例处理程序
            change = (e) => {
                this.setState({
                    keyword: e.target.getAttribute('k') 
                })
            }
        }
        ReactDOM.render(<App />, document.querySelector("#root"))

    </script>
</body>
</html>